#include "ipdialog.h"
#include <QApplication>

IPDialog::IPDialog(QWidget *parent) : QDialog(parent)
{
}


void IPDialog::buildWidget(const QString &addr, const int &port)
{
  QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);

  verticalLayout = new QVBoxLayout(this);
  verticalLayout->setContentsMargins(0, 0, 0, 0);

  titleWidget = new QLabel(this);
  sizePolicy.setHorizontalStretch(0);
  sizePolicy.setVerticalStretch(0);
  sizePolicy.setHeightForWidth(titleWidget->sizePolicy().hasHeightForWidth());
  titleWidget->setSizePolicy(sizePolicy);
  titleWidget->setAlignment(Qt::AlignCenter);

  verticalLayout->addWidget(titleWidget);
  titleaddressSpacer = new QSpacerItem(20, 2, QSizePolicy::Minimum, QSizePolicy::Fixed);
  verticalLayout->addItem(titleaddressSpacer);

  horizontalLayout = new QHBoxLayout();
  leftSpacer = new QSpacerItem(5, 20, QSizePolicy::Preferred, QSizePolicy::Minimum);

  horizontalLayout->addItem(leftSpacer);

  if(addr.length() == 0)
    ipAddressWidget = new QLineEdit(this);
  else
    ipAddressWidget = new QLineEdit(addr, this);

  horizontalLayout->addWidget(ipAddressWidget);

  addressseparator = new QLabel(this);
  horizontalLayout->addWidget(addressseparator);

  portWidget = new QLineEdit(QString(port), this);

  sizePolicy.setHeightForWidth(portWidget->sizePolicy().hasHeightForWidth());
  portWidget->setSizePolicy(sizePolicy);
  portWidget->setMinimumSize(QSize(50, 0));
  portWidget->setMaximumSize(QSize(70, 16777215));
  portWidget->setLayoutDirection(Qt::RightToLeft);
  horizontalLayout->addWidget(portWidget);

  rightSpacer = new QSpacerItem(5, 20, QSizePolicy::Preferred, QSizePolicy::Minimum);
  horizontalLayout->addItem(rightSpacer);
  verticalLayout->addLayout(horizontalLayout);


  buttonaddresSpacer = new QSpacerItem(20, 2, QSizePolicy::Minimum, QSizePolicy::Fixed);
  verticalLayout->addItem(buttonaddresSpacer);

  buttonBox = new QDialogButtonBox(this);
  buttonBox->setOrientation(Qt::Horizontal);
  buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
  verticalLayout->addWidget(buttonBox);

/******************************************** RETRANSLATE UI ************************************************************************/
  this->setWindowTitle(QApplication::translate("IpAddressDialog_context", "Dialog", 0, QApplication::UnicodeUTF8));
  titleWidget->setText(QApplication::translate("IpAddressDialog_context", "Enter IP Address", 0, QApplication::UnicodeUTF8));
  addressseparator->setText(QApplication::translate("IpAddressDialog_context", ":", 0, QApplication::UnicodeUTF8));
/************************************************************************************************************************************/

  QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
  QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

}

void IPDialog::accept()
{
  ipaddress_result result;

  result.addr = ipAddressWidget->text();
  result.port = portWidget->text().toInt();

  QDialog::accept();
//  connect(version, SIGNAL(currentIndexChanged(QString)), this, SLOT(moduleVersionIndexChanged(QString)));
  emit ipaccept(result);

}

void IPDialog::reject ()
{
  QDialog::reject();
}
